package com.demo.util;

import java.io.PrintWriter;
import java.math.BigDecimal;
import java.sql.Date;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.GregorianCalendar;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

import com.demo.common.Constants;


public class Util
{
	
	public static List<Object[]> addObject2ArrayList(List<Object[]> objectArrayList, Object object)
	{
		List<Object[]> list = null;
		
		if(Util.isNotEmpty(objectArrayList))
		{
			list = new ArrayList<Object[]>();
			for(Object[] objects: objectArrayList)
			{
				List objectList = Util.arrayToList(objects);
				if(objectList != null)
				{
					objectList.add(object);
					list.add(objectList.toArray());
				}
			}
		}
		
		return list;
	}
	
	
	/* @purpose:calculate the page count using the count and pagesize
	 * @params: int ,int
	 * @return: int
	 */
	public static int count2PageCount(int count, int pageSize)
	{
		int pageCount = count / pageSize;
		if(count % pageSize != 0)
		{
			pageCount++;
		}
		
		return pageCount;
	}
	
	public static int id2Action(int id)
	{
		if(id == -1)
		{
			return Constants.ADD_ACTION;
		}
		else
		{
			return Constants.UPDATE_ACTION;
		}
	}
	
	/* @purpose:put a List to String use the specified pattern ";"
	 * @params: List
	 * @return: String
	 */
	public static String listToString(List list)
	{
		StringBuffer stringBuffer = new StringBuffer();
		
		if(Util.isNotEmpty(list))
		{
			for(Object object: list)
			{
				if(object != null)
				{
					stringBuffer.append(object.toString());
					stringBuffer.append(";");
				}
			}
		}
		
		if(stringBuffer.length() == 0)
		{
			return null;
		}
		else
		{
			return stringBuffer.toString();
		}
	}
	
	/* @purpose:put a List to String use the specified pattern
	 * @params: List
	 * @return: String
	 */
	public static String listToString(List list, String pattern)
	{
		StringBuffer stringBuffer = new StringBuffer();
		
		if(Util.isNotEmpty(list))
		{
			for(Object object: list)
			{
				if(object != null)
				{
					stringBuffer.append(object.toString());
					stringBuffer.append(pattern);
				}
			}
		}
		
		if(stringBuffer.length() == 0)
		{
			return null;
		}
		else
		{
			return stringBuffer.toString();
		}
	}
	/* @purpose:get the xml document root element
	 * @params: 
	 * @return: Document
	 */
	public static Document getDocumentRoot()
	{
		Document document = DocumentHelper.createDocument();
		document.setXMLEncoding("UTF-8");
		document.addElement("root");
		return document;
	}
	
	
	/* @purpose:put a array to a String using the specified pattern ";"
	 * @params: Object[]
	 * @return: String
	 */
	public static String array2String(Object[] objects)
	{
		String string = null;

		StringBuffer stringBuffer = new StringBuffer();
		if(Util.isNotEmpty(objects))
		{
			for(Object object: objects)
			{
				if(object != null)
				{
					stringBuffer.append(object.toString());
					stringBuffer.append(";");
				}
			}
		}
		
		if(stringBuffer.length() > 0)
		{
			string = stringBuffer.toString();
		}
		
		return string;
	}
	
	/* @purpose:put a Object Array to List
	 * @params: Object[]
	 * @return: List
	 */
	public static List arrayToList(Object[] objects)
	{
		List list = null;
		
		if(Util.isNotEmpty(objects))
		{
			list = new ArrayList();
			for(Object object: objects)
			{
				if(object != null)
				{
					list.add(object);
				}
			}
		}
		
		return list;
	}
	
	/* @purpose:put a String array to List<Integer>
	 * @params: String[]
	 * @return: List<Integer>
	 */
	public static List<Integer> stringArrayToListInteger(String[] strings)
	{
		List<Integer> list = null;
		
		try
		{
			if(Util.isNotEmpty(strings))
			{
				list = new ArrayList<Integer>();
				for(String string: strings)
				{
					if(string != null)
					{
						list.add(Integer.parseInt(string));
					}
				}
			}
		}
		catch(Exception e)
		{
			e.printStackTrace();
			list = null;
		}
		
		
		return list;
	}
	
	/* @purpose:put a String to List<String> based on the specified pattern ";"
	 * @params: String
	 * @return: List<String>
	 */
	public static List<String> stringToList(String string)
	{
		List<String> list = null;
		
		if(Util.isNotEmpty(string))
		{
			String[] stringArray = string.split(";");
			list = arrayToList(stringArray);
		}
		
		return list;
	}
	
	public static boolean checkPrivilege(String privilegeList, List<String> userPrivilegeList)
	{
		boolean flag = false;
		
		if(!Util.isNotEmpty(privilegeList))
		{
			flag = true;
		}
		else
		{
			if(Util.isNotEmpty(userPrivilegeList))
			{
				String[] privilegeListArray = privilegeList.split("\\|");
				if(Util.isNotEmpty(privilegeListArray))
				{
					for(String privilegeListString: privilegeListArray)
					{
						if(Util.isNotEmpty(privilegeListString))
						{
							List<String> list = Util.stringToList(privilegeListString);
							if(Util.isNotEmpty(list))
							{
								if(userPrivilegeList.containsAll(list))
								{
									flag = true;
									break;
								}
							}
						}
					}
				}
			}
		}
		
		return flag;
	}
	
	public static void generatePrivilegeXml(Element root, List<String> privilegeList)
	{
		if(root != null && Util.isNotEmpty(privilegeList))
		{
			String privilegeString = Util.listToString(privilegeList);
			root.addElement("privilegeList").setText(Util.null2empty(privilegeString));
		}
	}
	
	/* @purpose:if a object is null the return "", else return itself
	 * @params: Object
	 * @return: String
	 */
	public static String null2empty(Object object)
	{
		if(object != null)
		{
			return object.toString();
		}
		else
		{
			return "";
		}
	}
	
	
	public static void responseXml(HttpServletResponse response, String xmlString) throws Exception
	{
		response.setContentType("text/XML;charset=UTF-8");
		PrintWriter out = response.getWriter();
		out.write(xmlString);
		out.close();
	}
	
	/* @purpose:check if the list is empty
	 * @params: List
	 * @return: boolean
	 */
	public static boolean isNotEmpty(List list)
	{
		boolean returnBoolean = false;
		if(list != null && list.size() > 0)
		{
			returnBoolean = true;
		}
		
		return returnBoolean;
	}
	
	/* @purpose:check if the Object[] is empty
	 * @params: Object[]
	 * @return: boolean
	 */
	public static boolean isNotEmpty(Object[] ObjectArray)
	{
		boolean returnBoolean = false;
		if(ObjectArray != null && ObjectArray.length > 0)
		{
			returnBoolean = true;
		}
		
		return returnBoolean;
	}
	
	/* @purpose:check if the String...Strings is empty
	 * @params: String list
	 * @return: boolean
	 */
	public static boolean isNotEmpty(String...strings)
	{
		boolean returnBoolean = true;
		
		if(strings != null && strings.length > 0)
		{
			for(String string : strings)
			{
				if(string == null || "".equals(string))
				{
					returnBoolean = false;
					break;
				}
			}
		}
		else
		{
			returnBoolean = false;
		}
		
		return returnBoolean;
	}
	
	/* @purpose:check if the number is valid (invalid=-1)
	 * @params: Integer
	 * @return: boolean
	 */
	public static boolean isValid(Integer...numbers)
	{
		boolean returnBoolean = true;
		
		if(numbers != null && numbers.length > 0)
		{
			for(Integer number : numbers)
			{
				if(number == null || number == -1)
				{
					returnBoolean = false;
					break;
				}
			}
		}
		else
		{
			returnBoolean = false;
		}
		
		return returnBoolean;
	}
	
	/* @purpose:calculate the average number
	 * @params: double, int ,int
	 * @return: boolean
	 */
	public static double div(double d1, int d2, int scale){
		BigDecimal bd2 = new BigDecimal(d2*10000);
		BigDecimal bd1 = new BigDecimal(d1);
		if (d1 != 0){
			return bd2.divide(bd1, scale, BigDecimal.ROUND_HALF_UP).doubleValue();
		}
		return 0;
	}
	
	/* @purpose:get online date
	 * @params: 
	 * @return: Date
	 */
	public static java.util.Date getOnlineBusinessDate(){
		SimpleDateFormat sdfInput = new SimpleDateFormat("dd/MM/yyyy");
		try{
			java.util.Date cdate = sdfInput.parse(sdfInput.format(System.currentTimeMillis()));
			return (new Date(cdate.getTime()));
		}
		catch(ParseException px){
			
		}
		return null;
	}
	
	/* @purpose:get online date
	 * @params: 
	 * @return: String
	 */
	public static String getOnlineBusinessDateStr(){
		SimpleDateFormat sdfInput = new SimpleDateFormat("dd/MM/yyyy");
		String currentDate = sdfInput.format(System.currentTimeMillis());
		return currentDate;
	}
	
	/* @purpose:get online timestamp
	 * @params: 
	 * @return: String
	 */
	public static String getOnlineBusinessTimestampStr(){
		SimpleDateFormat sdfInput = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
		String currentTimestamp = sdfInput.format(System.currentTimeMillis());
		return currentTimestamp;
	}
	
	/* @purpose:get specified date using the specified pattern
	 * @params: String, String
	 * @return: Date
	 */
	public static java.util.Date getSpecifiedDate(String specifiedDate, String pattern){
		SimpleDateFormat sdfInput = new SimpleDateFormat(pattern);
		try{
			java.util.Date cdate = sdfInput.parse(specifiedDate);
			return (new Date(cdate.getTime()));
		}
		catch(ParseException px){
			
		}
		return null;
	}
	
	/* @purpose:get specified date using the specified pattern "dd/MM/yyy"
	 * @params: String
	 * @return: Date
	 */
	public static java.util.Date getSpecifiedDate(String specifiedDate){
		SimpleDateFormat sdfInput = new SimpleDateFormat("dd/MM/yyyy");
		try{
			java.util.Date cdate = sdfInput.parse(specifiedDate);
			return (new Date(cdate.getTime()));
		}
		catch(ParseException px){
			
		}
		return null;
	}
	
	/* @purpose:get specified Timestamp using the specified pattern
	 * @params: String, String
	 * @return: Timestamp
	 */
	public static Timestamp getSpecifiedTimestamp(String specifiedDateTimestamp, String pattern){
		SimpleDateFormat sdfInput = new SimpleDateFormat(pattern);
		try{
			java.util.Date cdate = sdfInput.parse(specifiedDateTimestamp);
			return (new Timestamp(cdate.getTime()));
		}
		catch(ParseException px){
			
		}
		return null;
		
	}
	
	/* @purpose:get specified Timestamp using the specified pattern "dd/MM/yyyy HH:mm:ss"
	 * @params: String
	 * @return: Timestamp
	 */
	public static Timestamp getSpecifiedTimestamp(String specifiedDateTimestamp){
		SimpleDateFormat sdfInput = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
		try{
			java.util.Date cdate = sdfInput.parse(specifiedDateTimestamp);
			return (new Timestamp(cdate.getTime()));
		}
		catch(ParseException px){

		}
		return null;
		
	}
	
	/* @purpose:add the specifed year on the date
	 * @params: Date, int
	 * @return: Date
	 */
	public static java.util.Date addYear(java.util.Date date, int year){
		GregorianCalendar gdate = new GregorianCalendar();
		gdate.setTime(date);
		gdate.add(GregorianCalendar.YEAR, year);
		return gdate.getTime();
	}
	
	/* @purpose:add the specifed month on the date
	 * @params: Date, int
	 * @return: Date
	 */
	public static java.util.Date addMonth(java.util.Date date, int month){
		GregorianCalendar gdate = new GregorianCalendar();
		gdate.setTime(date);
		gdate.add(GregorianCalendar.MONTH, month);
		return gdate.getTime();
	}
	
	/* @purpose:add the specifed day on the date
	 * @params: Date, int
	 * @return: Date
	 */
	public static java.util.Date addDay(java.util.Date date, int day){
		GregorianCalendar gdate = new GregorianCalendar();
		gdate.setTimeInMillis(date.getTime());
		gdate.add(GregorianCalendar.DAY_OF_MONTH, day);
		return gdate.getTime();
	}
	
}
